module executor.message.client.UnrecevieMsgExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import client.ImClient;
import std.array;
import command.StartAppCommand;
import std.conv: to;
import hunt.logging;
import hunt.util.Serialize;

@TransportClient(logicClientName)
class UnrecevieMsgExecutor : AbstractExecutor!(UnrecevieMsgExecutor) {

  @MessageId(MESSAGE.CMD_UNRECEIVE_RES)
  void unrecevie(TransportContext ctx, MessageBuffer buffer)
  {
    auto res = new UnreceivedMessageRES();
    buffer.data.fromProtobuf!UnreceivedMessageRES(res);
    TransportSession session = ImClient.getInstance().getImClientByMixId(res.clientId);
    if (session !is null)
    {
      logInfo("send offlineMsg");
      session.send(new MessageBuffer(MESSAGE.CMD_UNRECEIVE_RES,res.toProtobuf.array));
    } else
    {
      logWarning("client id :%s is closed",res.clientId);
    }
  }

  @MessageId(MESSAGE.CMD_GET_HISTORY_RES)
  void getHistoryMsg(TransportContext ctx, MessageBuffer buffer)
  {
    auto res = new HistoryMsgRES();
    buffer.data.fromProtobuf!HistoryMsgRES(res);
    TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.clientId), res.src));
    logInfo("sendMsg : %d",res.clientId);
    if (client !is null)
    {
      client.send(new MessageBuffer(MESSAGE.CMD_GET_HISTORY_RES,res.toProtobuf.array));
    }
  }


  @MessageId(MESSAGE.CMD_GET_SESSION_LIST_RES)
  void sessionList(TransportContext ctx, MessageBuffer buffer)
  {
    auto res = new SessionListRes();
    buffer.data.fromProtobuf!SessionListRes(res);
    logInfo("sendMsg : %d",toJson(res));
    TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(to!ulong(res.clientId), res.src));
    if (client !is null)
    {
      client.send( new MessageBuffer(MESSAGE.CMD_GET_SESSION_LIST_RES,res.toProtobuf.array));
    }
  }


}

